<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>chai.js笔记 | latte and cat</title>
    <meta name="description" content="study programs and record life">
    <meta name="generator" content="VuePress 1.4.0">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.e1c67194.css" as="style"><link rel="preload" href="/assets/js/app.ff77de4f.js" as="script"><link rel="preload" href="/assets/js/4.6c1b7be2.js" as="script"><link rel="preload" href="/assets/js/1.3ea07518.js" as="script"><link rel="preload" href="/assets/js/15.652b11fc.js" as="script"><link rel="prefetch" href="/assets/js/10.18024e3f.js"><link rel="prefetch" href="/assets/js/11.a9ba7471.js"><link rel="prefetch" href="/assets/js/12.64cdb496.js"><link rel="prefetch" href="/assets/js/13.9fb4b156.js"><link rel="prefetch" href="/assets/js/14.c3753656.js"><link rel="prefetch" href="/assets/js/16.b24685ca.js"><link rel="prefetch" href="/assets/js/17.67155a52.js"><link rel="prefetch" href="/assets/js/18.1d703305.js"><link rel="prefetch" href="/assets/js/19.0b6a6748.js"><link rel="prefetch" href="/assets/js/20.a576349c.js"><link rel="prefetch" href="/assets/js/21.4bd94152.js"><link rel="prefetch" href="/assets/js/22.1abe4122.js"><link rel="prefetch" href="/assets/js/23.d2352828.js"><link rel="prefetch" href="/assets/js/24.a69cf77c.js"><link rel="prefetch" href="/assets/js/25.40b2c71a.js"><link rel="prefetch" href="/assets/js/26.7da4b904.js"><link rel="prefetch" href="/assets/js/27.80a148eb.js"><link rel="prefetch" href="/assets/js/28.d8cf3072.js"><link rel="prefetch" href="/assets/js/5.60884555.js"><link rel="prefetch" href="/assets/js/6.e3bfbe92.js"><link rel="prefetch" href="/assets/js/7.f4dfd5c4.js"><link rel="prefetch" href="/assets/js/8.14ec5fab.js"><link rel="prefetch" href="/assets/js/9.e4d60889.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.af2c2885.js">
    <link rel="stylesheet" href="/assets/css/0.styles.e1c67194.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-319dd33c><div data-v-319dd33c><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-319dd33c data-v-319dd33c><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>latte and cat</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>橘子</span>
            
          <span data-v-1e2a0cc0>2019 - </span>
          2020
        </a></span></div></div> <div class="hide" data-v-319dd33c><header class="navbar" data-v-319dd33c><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/head.png" alt="latte and cat" class="logo"> <span class="site-name">latte and cat</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/摘记/" class="nav-link"><i class="iconfont undefined"></i>
  摘记
</a></li><li class="dropdown-item"><!----> <a href="/categories/随笔/" class="nav-link"><i class="iconfont undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/note/" class="nav-link router-link-active"><i class="iconfont reco-document"></i>
  Notes
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="http://photo.smallsunnyfox.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-blog"></i>
  PhotoAlbum
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/smallsunnyfox" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-319dd33c></div> <aside class="sidebar" data-v-319dd33c><div class="personal-info-wrapper" data-v-6c8ffc9c><!----> <h3 class="name" data-v-6c8ffc9c>
    橘子
  </h3> <div class="num" data-v-6c8ffc9c><div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>14</h3> <h6 data-v-6c8ffc9c>文章</h6></div> <div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>6</h3> <h6 data-v-6c8ffc9c>标签</h6></div></div> <hr data-v-6c8ffc9c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/摘记/" class="nav-link"><i class="iconfont undefined"></i>
  摘记
</a></li><li class="dropdown-item"><!----> <a href="/categories/随笔/" class="nav-link"><i class="iconfont undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/note/" class="nav-link router-link-active"><i class="iconfont reco-document"></i>
  Notes
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="http://photo.smallsunnyfox.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-blog"></i>
  PhotoAlbum
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/smallsunnyfox" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>HTML5</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/html5/HTML5的语义元素.html" class="sidebar-link">HTML5语义元素</a></li><li><a href="/note/html5/HTML5多媒体标签.html" class="sidebar-link">HTML5多媒体标签</a></li><li><a href="/note/html5/HTML5表单元素.html" class="sidebar-link">HTML5表单元素</a></li><li><a href="/note/html5/HTML5中的API.html" class="sidebar-link">HTML5中的API</a></li><li><a href="/note/html5/Canvas.html" class="sidebar-link">Canvas</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>CSS</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/css/css3新特性.html" class="sidebar-link">CSS3新特性</a></li><li><a href="/note/css/css3Flip.html" class="sidebar-link">CSS3实现翻转(Flip)效果</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>前端单元测试</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/fe-unit-test/chai.html" class="active sidebar-link">chai.js笔记</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/note/fe-unit-test/chai.html#bdd" class="sidebar-link">BDD</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/note/fe-unit-test/chai.html#介绍" class="sidebar-link">介绍</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/chai.html#语言链" class="sidebar-link">语言链</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/chai.html#断言" class="sidebar-link">断言</a></li></ul></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/chai.html#tdd" class="sidebar-link">TDD</a></li></ul></li><li><a href="/note/fe-unit-test/mocha.html" class="sidebar-link">Mocha笔记</a></li><li><a href="/note/fe-unit-test/vueTestUtils.html" class="sidebar-link">VueTestUtils笔记</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>chai.js笔记</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>橘子</span>
            
          <span data-v-1e2a0cc0>2019 - </span>
          2020
        </a></span></div></div> <div data-v-319dd33c><main class="page"><div class="page-title" style="display:none;"><h1>chai.js笔记</h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>橘子</span></i> <!----> <i class="iconfont reco-eye" data-v-484a899e><span id="/note/fe-unit-test/chai.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-484a899e><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-484a899e><span class="tag-item" data-v-484a899e>
      单元测试
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="bdd"><a href="#bdd" class="header-anchor">#</a> BDD</h2> <h3 id="介绍"><a href="#介绍" class="header-anchor">#</a> 介绍</h3> <p>expect和should是BDD风格的，二者使用相同的链式语言来组织断言</p> <p>但不同在于他们初始化断言的方式：</p> <ul><li>expect使用构造函数来创建断言对象实例</li> <li>should通过为Object.prototype新增方法来实现断言（所以should不支持IE）</li> <li>expect直接指向chai.expect</li> <li>should则是chai.should()。</li></ul> <h3 id="语言链"><a href="#语言链" class="header-anchor">#</a> 语言链</h3> <p>下面的接口是单纯作为语言链提供以期提高断言的可读性。除非被插件改写否则它们一般不提供测试功能</p> <ul><li><p>to</p></li> <li><p>be</p></li> <li><p>been</p></li> <li><p>is</p></li> <li><p>that</p></li> <li><p>which</p></li> <li><p>and</p></li> <li><p>has</p></li> <li><p>have</p></li> <li><p>with</p></li> <li><p>at</p></li> <li><p>of</p></li> <li><p>same</p></li></ul> <h3 id="断言"><a href="#断言" class="header-anchor">#</a> 断言</h3> <ul><li><p>.not：对之后的断言取反</p></li> <li><p>.deep</p> <ul><li>设置deep标记，然后使用equal和property断言。</li> <li>该标记可以让其后的断言不是比较对象本身，而是递归比较对象的键值对</li></ul></li> <li><p>.any：在<code>keys</code>断言之前使用<code>any</code>标记（与<code>all</code>相反）</p></li> <li><p>.all：在<code>keys</code>断言之前使用<code>all</code>标记（与<code>any</code>相反）</p></li> <li><p>.a(type) / .an(type)</p> <ul><li>type为被测试的值的类型</li> <li><code>a</code>和<code>an</code>断言即可作为语言链又可作为断言使用</li></ul></li> <li><p>.include(value)  /  .contains(value)</p> <ul><li>value: Object | String | Number</li> <li><code>include()</code>和<code>contains()</code>即可作为属性类断言前缀语言链又可作为作为判断数组、字符串是否包含某值的断言使用。当作为语言链使用时，常用于<code>key()</code>断言之前</li></ul></li> <li><p>.ok：断言其目标为<code>真值</code></p></li> <li><p>.true： 断言目标为<code>true</code>，注意，这里与<code>ok</code>的区别是不进行类型转换，只能为<code>true</code>才能通过断言</p></li> <li><p>.false：断言目标为<code>false</code></p></li> <li><p>.null：断言目标为<code>null</code></p></li> <li><p>.undefined：断言目标为<code>undefined</code></p></li> <li><p>.NaN：断言目标为非数字<code>NaN</code></p></li> <li><p>.exist：断言目标存在，即非<code>null</code>也非<code>undefined</code></p></li> <li><p>.empty</p> <ul><li>断言目标的长度为<code>0</code>。</li> <li>对于数组和字符串，它检查<code>length</code>属性</li> <li>对于对象，它检查可枚举属性的数量</li></ul></li> <li><p>.arguments：断言目标是一个参数对象<code>arguments</code></p></li> <li><p>.equal(value)</p> <ul><li>value：Mixed</li> <li>断言目标严格等于(<code>===</code>)<code>value</code>。</li> <li>如果设置了<code>deep</code>标记，则断言目标深度等于<code>value</code></li></ul></li> <li><p>.eql(value)</p> <ul><li>value：Mixed</li> <li>断言目标深度等于<code>value</code>，相当于<code>deep.equal(value)</code>的简写</li></ul></li> <li><p>.above(value)</p> <ul><li>value：Number</li> <li>断言目标大于（超过）<code>value</code></li> <li>也可接在<code>length</code>后来断言一个最小的长度。相比直接提供长度的好处是提供了更详细的错误消息</li></ul></li> <li><p>.least(value)</p> <ul><li>value：Number</li> <li>断言目标不小于（大于或等于）<code>value</code></li> <li>也可接在<code>length</code>后来断言一个最小的长度。相比直接提供长度的好处是提供了更详细的错误消息</li></ul></li> <li><p>.below(value)</p> <ul><li>value：Number</li> <li>断言目标小于<code>value</code></li> <li>也可接在length后来断言一个最大的长度。相比直接提供长度的好处是提供了更详细的错误消息</li></ul></li> <li><p>.most(value)</p> <ul><li>value：String</li> <li>断言目标不大于（小于或等于）<code>value</code></li> <li>也可接在length后来断言一个最大的长度。相比直接提供长度的好处是提供了更详细的错误消息</li></ul></li> <li><p>.within(start, finish)</p> <ul><li>start：Number，下限</li> <li>finish：Number，上限</li> <li>断言目标在某个区间内</li> <li>也可接在length后来断言一个长度区间。相比直接提供长度的好处是提供了更详细的错误消息</li></ul></li> <li><p>.instanceof(constructor)</p> <ul><li>constructor: Constructor构造函数</li> <li>断言目标是构造函数的一个实例</li></ul></li> <li><p>.property(name, [value])</p> <ul><li><p>name：String，属性名</p></li> <li><p>value：Mixed，可选，属性值</p></li> <li><p>断言目标是否拥有某个名为<code>name</code>的属性，可选地如果提供了<code>value</code>则该属性值还需要严格等于（<code>===</code>）<code>value</code>。</p></li> <li><p>如果设置了<code>deep</code>标记，则可以使用点<code>.</code>和中括号<code>[]</code>来指向对象和数组中的深层属性</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 简单引用</span>
<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span> foo<span class="token operator">:</span> <span class="token string">'bar'</span> <span class="token punctuation">}</span>
<span class="token function">expect</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'foo'</span><span class="token punctuation">)</span>
<span class="token function">expect</span><span class="token punctuation">(</span>pbj<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token string">'bar'</span><span class="token punctuation">)</span>

<span class="token comment">// 深层引用</span>
<span class="token keyword">var</span> deepObj <span class="token operator">=</span> <span class="token punctuation">{</span>
  green<span class="token operator">:</span> <span class="token punctuation">{</span> tea<span class="token operator">:</span> <span class="token string">'matcha'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  teas<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">'Chai'</span><span class="token punctuation">,</span> <span class="token string">'matcha'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> tea<span class="token operator">:</span> <span class="token string">'konacha'</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token function">expect</span><span class="token punctuation">(</span>deepObj<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'green.tea'</span><span class="token punctuation">,</span> <span class="token string">'matcha'</span><span class="token punctuation">)</span>
<span class="token function">expect</span><span class="token punctuation">(</span>deepObj<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'teas[1]'</span><span class="token punctuation">,</span> <span class="token string">'matcha'</span><span class="token punctuation">)</span>
<span class="token function">expect</span><span class="token punctuation">(</span>deepObj<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'teas[2].tea'</span><span class="token punctuation">,</span> <span class="token string">'konacha'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></li> <li><p>如果目标是一个数组，还可以直接使用一个或多个数组下标作为<code>name</code>来在嵌套数组中断言<code>deep.property</code></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">[</span> <span class="token string">'chai'</span><span class="token punctuation">,</span> <span class="token string">'matcha'</span><span class="token punctuation">,</span> <span class="token string">'konacha'</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">[</span> <span class="token punctuation">{</span> tea<span class="token operator">:</span> <span class="token string">'chai'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> tea<span class="token operator">:</span> <span class="token string">'matcha'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> tea<span class="token operator">:</span> <span class="token string">'konacha'</span> <span class="token punctuation">}</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">]</span>
<span class="token function">expect</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'[0][1]'</span><span class="token punctuation">,</span> <span class="token string">'matcha'</span><span class="token punctuation">)</span>
<span class="token function">expect</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'[1][2].tea'</span><span class="token punctuation">,</span> <span class="token string">'konacha'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li> <li><p><code>property</code>把断言的主语（subject）从原来的对象变为当前属性的值，使得可以在其后进一步衔接其它链式断言（来针对这个属性值进行测试）</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">expect</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'foo'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span>that<span class="token punctuation">.</span>is<span class="token punctuation">.</span><span class="token function">a</span><span class="token punctuation">(</span><span class="token string">'string'</span><span class="token punctuation">)</span>
<span class="token function">expect</span><span class="token punctuation">(</span>deepObj<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'green'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span>that<span class="token punctuation">.</span>is<span class="token punctuation">.</span><span class="token function">an</span><span class="token punctuation">(</span><span class="token string">'object'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span>that<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token punctuation">{</span> tea<span class="token operator">:</span> <span class="token string">'matcha'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token function">expect</span><span class="token punctuation">(</span>deepObj<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'teas'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span>that<span class="token punctuation">.</span>is<span class="token punctuation">.</span><span class="token function">an</span><span class="token punctuation">(</span><span class="token string">'array'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span>with<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'[2]'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span>that<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token punctuation">{</span> tea<span class="token operator">:</span> <span class="token string">'konacha'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li> <li><p>只有当设置了<code>deep</code>标记的时候，在<code>property()</code> <code>name</code>中的点（<code>.</code>）和中括号（<code>[]</code>）才必须使用双反斜杠<code>\</code>进行转义</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 简单指向</span>
<span class="token keyword">var</span> css <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'.link[target]'</span><span class="token operator">:</span> <span class="token number">42</span> <span class="token punctuation">}</span>
<span class="token function">expect</span><span class="token punctuation">(</span>css<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'.link[target]'</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span>

<span class="token comment">//深度指向</span>
<span class="token keyword">var</span> deepCss <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">'link'</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token string">'[target]'</span><span class="token operator">:</span> <span class="token number">42</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
<span class="token function">expect</span><span class="token punctuation">(</span>deepCss<span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>have<span class="token punctuation">.</span>deep<span class="token punctuation">.</span><span class="token function">property</span><span class="token punctuation">(</span><span class="token string">'\\.link\\.[target]'</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li></ul></li> <li><p>.ownProperty(name)</p> <ul><li>name：String，属性名</li> <li>断言目标拥有名为<code>name</code>的自有属性</li></ul></li> <li><p>.ownPropertyDescription(name, [descriptor])</p> <ul><li>name：String，属性名</li> <li>descriptor：Object，描述对象，可选</li> <li>断言目标的某个自有属性存在描述符对象，如果给定了<code>descroptor</code>描述符对象，则该属性的描述符对象必须与其相匹配</li></ul></li> <li><p>.length：设置<code>.have.length</code>标记作为比较<code>length</code>属性值的前缀</p></li> <li><p>.lengthOf(value)</p> <ul><li>value：Number</li> <li>断言目标的<code>length</code>属性为期望的值</li></ul></li> <li><p>.match(regexp)</p> <ul><li>regexp：RegExp，正则表达式</li> <li>断言目标匹配到一个正则表达式</li></ul></li> <li><p>.string(string)</p> <ul><li>string：String，字符串</li> <li>断言目标字符串包含另一个字符串</li></ul></li> <li><p>.keys(key1, [key2], [...])</p> <ul><li>key：String | Array | Object 属性名</li> <li>断言目标包含传入的属性名</li> <li>与<code>any</code>，<code>all</code>，<code>contains</code>或者<code>have</code>前缀结合使用会影响测试结果：
<ul><li>当与<code>any</code>结合使用时，无论是使用<code>have</code>还是使用<code>contains</code>前缀，目标必须至少存在一个传入的属性名才能通过测试。注意，<code>any</code>或者<code>all</code>应当至少使用一个，否则默认为<code>all</code></li> <li>当结合<code>all</code>和<code>contains</code>使用时，目标对象必须至少拥有全部传入的属性名，但是它也可以拥有其它属性名</li> <li>当结合<code>all</code>和<code>have</code>使用时，目标对象必须且仅能拥有全部传入的属性名</li></ul></li></ul></li> <li><p>.throw(constructor)</p> <ul><li>constructor：ErrorConstructor | String | RegExp</li> <li>断言目标函数会抛出一个指定错误或错误类型（使用<code>instanceOf</code>计算），也可使用正则表达式或者字符串来检测错误消息</li> <li>注意，当一个抛错断言被否定了（前面有<code>.not</code>），那么它会从Error构造函数开始依次检查各个可能传入的参数。检查一个只是消息类型不匹配但是已知的错误，合理的方式是先断言该错误存在，然后使用<code>.and</code>后断言错误消息不匹配</li></ul></li> <li><p>.respondTo(method)</p> <ul><li>method：String</li> <li>断言目标类或对象会响应一个方法（存在这个方法）</li></ul></li> <li><p>.itself</p> <ul><li>设置<code>itself</code>标记，然后使用<code>respondTo</code>断言</li> <li>用于检查一个构造函数是否会响应一个静态方法（挂载在构造函数本身的方法）</li></ul></li> <li><p>.satisfy(method)</p> <ul><li>method：Function，测试器，接受一个参数表示目标值，返回一个布尔值</li> <li>断言目标值能够让给定的测试器返回真值</li></ul></li> <li><p>.closeTo(expected, delta)</p> <ul><li>expect：Numbre，期望值</li> <li>delta：Numbre，范围半径</li> <li>断言目标数字等于<code>expected</code>，或在期望值的+/-<code>delta</code>范围内</li></ul></li> <li><p>.members(set)</p> <ul><li>set：Array</li> <li>断言目标是<code>set</code>的超集，或前者有后者所有严格相等（<code>===</code>）的成员。另外，如果设置了<code>deep</code>标记，则成员进行深度比较（include/contains只能接受单个值，但它们的主语除了是数组，还可以判断字符串；members则将它们的能力扩展为能够接受一个数组，但主语只能是数组）</li></ul></li> <li><p>.oneOf(list)</p> <ul><li>list：Array</li> <li>断言目标值出现在<code>list</code>数组的某个顶层位置（直接子元素，严格相等）</li></ul></li> <li><p>.change(object, property)</p> <ul><li>object：Object，对象</li> <li>property：String，属性名</li> <li>断言目标方法会改变指定对象的指定属性</li></ul></li> <li><p>.increase(object, property)</p> <ul><li>object：Object，对象</li> <li>property：String，属性名</li> <li>断言目标方法会增加指定对象的属性</li></ul></li> <li><p>.decrease(object, property)</p> <ul><li>object：Object，对象</li> <li>property：String，属性名</li> <li>断言目标方法会减少指定对象的属性</li></ul></li> <li><p>.extensible：断言目标对象是可扩展的（可以添加新的属性）</p></li> <li><p>.sealed：断言目标对象是封闭的（无法添加新的属性并且存在的属性不能被删除但可以被修改）</p></li> <li><p>.frozen：断言目标对象是冻结的（无法添加新的属性并且存在的属性不能被删除和修改）</p></li></ul> <h2 id="tdd"><a href="#tdd" class="header-anchor">#</a> <a href="https://www.chaijs.com/api/assert/" target="_blank" rel="noopener noreferrer">TDD<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h2></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----></main> <!----> <div style="display:none;" data-v-319dd33c data-v-319dd33c><div class="comments-wrapper" data-v-319dd33c><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div></div></div>
    <script src="/assets/js/app.ff77de4f.js" defer></script><script src="/assets/js/4.6c1b7be2.js" defer></script><script src="/assets/js/1.3ea07518.js" defer></script><script src="/assets/js/15.652b11fc.js" defer></script>
  </body>
</html>
